<?php
declare (strict_types=1);

namespace addons\V2\admin\model;

use app\common\model\Base;

class ProductSpecValue extends Base
{

    protected $name = 'product_spec_value';

    public function product()
    {
        return $this->hasOne(Product::class, 'id', 'pid')
            ->field('id,title,cate_1,cate_2,icon');
    }

    public static function getSettled($param, $user): array
    {
        try {
            if (!$param['num'] || $param['num'] < 1) {
                return ['code' => -1, 'msg' => '购买数量错误'];
            }
            $specValue = parent::where('id', $param['spec_value_id'])->find();
            if (empty($specValue)) {
                return ['code' => -1, 'msg' => '未查询到相关规格'];
            }
            if ($param['num'] > $specValue->stock) {
                return ['code' => -1, 'msg' => '商品库存不足'];
            }
            $data['product'] = [
                [
                    'pid' => $specValue->pid,
                    'title' => $specValue->product->title,
                    'icon' => $specValue->product->icon,
                    'spec_value_id' => $specValue->id,
                    'vip_price' => $specValue->vip_price,
                    'points_amount' => $specValue->points_amount,
                    'sku_name' => SpecItem::getSpecItemName($specValue->sku_id),
                    'num' => $param['num'],
                    'cate_1' => $specValue->product->cate_1
                ]
            ];
            $data['total'] = $specValue->vip_price * $param['num'];
            $data['cate_1'] = [$specValue->product->cate_1];
            $data['realTotal'] = $data['total'];
            return ['code' => 1, 'msg' => 'success', 'data' => $data];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => '服务器错误：' . $e->getMessage()];
        }
    }

    public static function getSettledByProduct($param): array
    {
        try {
            $product = Product::where(['id' => $param['pid'], 'status' => 1])->find();
            if (empty($product)) {
                return ['code' => -1, 'msg' => '商品已下架'];
            }
            if (!$param['num'] || $param['num'] < 1) {
                return ['code' => -1, 'msg' => '购买数量错误'];
            }
            if ($param['num'] > $product->stock) {
                return ['code' => -1, 'msg' => '商品库存不足'];
            }
            $data['product'] = [
                [
                    'pid' => $product->id,
                    'title' => $product->title,
                    'icon' => $product->icon,
                    'points_amount' => $product->points_amount,
                    'vip_price' => $product->vip_price,
                    'spec_value_id' => '',
                    'sku_name' => '',
                    'num' => $param['num'],
                    'cate_1' => $product->cate_1
                ]
            ];
            $data['cate_1'] = [$product->cate_1];
            $data['total'] = $product->vip_price * $param['num'];

            $data['freightTotal'] = $product->freight * $param['num'];
            $data['realTotal'] = $data['total'] + $data['freightTotal'];
            return ['code' => 1, 'msg' => 'success', 'data' => $data];
        } catch (\Exception $e) {
            return ['code' => -1, 'msg' => '服务器错误：' . $e->getMessage()];
        }
    }

    public static function setSpecValue($pid, $data)
    {
        try {
            parent::destroy(['pid' => $pid]);
            $all = [];
            $vip_price = [];
            $points_amount = [];
            $stock = 0;
            foreach ($data as $k => $v) {
                $key['sku_id'] = $k;
                $key['pid'] = $pid;
                $key['stock'] = $v['stock'];
                $key['points_amount'] = $v['points_amount'];
                $key['vip_price'] = $v['vip_price'];
                $key['icon'] = $v['icon'];
                $all[] = $key;
                $stock += $v['stock'];
                $vip_price[] = $v['vip_price'];
                $points_amount[] = $v['points_amount'];
            }
            (new self())->insertAll($all);
            return [
                'stock' => $stock,
                'vip_price' => min($vip_price),
                'points_amount' => min($points_amount)
            ];
        } catch (\Exception $e) {
            return false;
        }
    }

    public static function getSpecValue($pid)
    {
        try {

            $list = parent::where('pid', $pid)->select();
            $skuData = [];
            foreach ($list as $v) {
                $skuData['skus[' . $v['sku_id'] . '][stock]'] = $v['stock'];
                $skuData['skus[' . $v['sku_id'] . '][icon]'] = $v['icon'];
                $skuData['skus[' . $v['sku_id'] . '][points_amount]'] = $v['points_amount'];
                $skuData['skus[' . $v['sku_id'] . '][vip_price]'] = $v['vip_price'];
            }
            return $skuData;
        } catch (\Exception $e) {

            return [];
        }
    }
}
